package flower.com.fenzu35011111.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import flower.com.fenzu35011111.R;
import flower.com.fenzu35011111.adapter.CJ_Adapter;
import flower.com.fenzu35011111.bean.Groups;
import flower.com.fenzu35011111.bean.Optionals;
import flower.com.fenzu35011111.bean.ShuJu;
import flower.com.fenzu35011111.db.Z_Groups_Dao;
import flower.com.fenzu35011111.db.Z_Optionals_Dao;
import flower.com.fenzu35011111.http.API;
import flower.com.fenzu35011111.http.JSONUtils;
import flower.com.fenzu35011111.http.Okhttp;

/**
 * Created by acer on 2019/6/7.
 */

public class ListViewActivity extends Activity {
    View getlistview;
    List<Boolean> bl = new ArrayList<>();
    ArrayList<Map<String, Object>> mData = new ArrayList<Map<String, Object>>();
    AlertDialog.Builder builder;
    AlertDialog builder2;
    SimpleAdapter adapter;
    //    Boolean[] bl = {false, false, false};
    ListView lv;
    CJ_Adapter cj_adapter;
    Z_Groups_Dao z_groups_dao;
    Z_Optionals_Dao z_optionals_dao;
    List<Groups> queryall;
    Context context;
    AlertDialog show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_main2);
        z_groups_dao = new Z_Groups_Dao(this);
        z_optionals_dao = new Z_Optionals_Dao(this);

//        Button button = (Button) findViewById(R.id.btn);
        lv = (ListView) findViewById(R.id.lv);
        cj_adapter = new CJ_Adapter();
        cj_adapter.addactivity(this);
        lv.setAdapter(cj_adapter);


        getmessage();

    }

    ShuJu.DataBean dataBean;

    public void position(String positions) {
        dataBean = data.get(Integer.valueOf(positions));
        CreateDialog();
    }

    public void addposition() {
        show.dismiss();
        addgroup();
    }


    List<ShuJu.DataBean> data;

    public void getmessage() {
        API.VehicleTrack(new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                ShuJu shuJu = JSONUtils.parseJSON(st, ShuJu.class);
                data = shuJu.getData();
                cj_adapter.addItem(data);
            }

            @Override
            public void onFalia(int code, String errst) {
//                Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
            }
        });
    }

    class ItemOnClick implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
            CheckBox cBox = (CheckBox) view.findViewById(R.id.X_checkbox);
            if (cBox.isChecked()) {
                cBox.setChecked(false);
            } else {
                Log.i("TAG", "取消该选项");
                cBox.setChecked(true);
            }


            if (cBox.isChecked()) {
                bl.set(position, true);
            } else {
                bl.set(position, false);

            }
        }

    }

    public void CreateDialog() {
        queryall = z_groups_dao.queryall();
        queryall.remove(0);
        int lengh = queryall.size();
        mData.clear();
        bl.clear();
        for (int i = 0; i < queryall.size(); i++) {
            bl.add(false);
        }


        // 动态加载一个listview的布局文件进来
        LayoutInflater inflater = LayoutInflater.from(ListViewActivity.this);
        getlistview = inflater.inflate(R.layout.listview, null);

        // 给ListView绑定内容
        final android.widget.ListView listview = (android.widget.ListView) getlistview.findViewById(R.id.X_listview);
        TextView textView = (TextView) getlistview.findViewById(R.id.tv);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(ListViewActivity.this, "添加分組", Toast.LENGTH_SHORT).show();
                addposition();
            }
        });


        for (int i = 0; i < lengh; i++) {
            Map<String, Object> item = new HashMap<String, Object>();
            item.put("text", queryall.get(i).getName());
            mData.add(item);
        }
        adapter = new SetSimpleAdapter(ListViewActivity.this, mData, R.layout.listitem, new String[]{"text"},
                new int[]{R.id.X_item_text});
        // 给listview加入适配器
        listview.setAdapter(adapter);
        listview.setItemsCanFocus(false);
        listview.setChoiceMode(android.widget.ListView.CHOICE_MODE_MULTIPLE);
        listview.setOnItemClickListener(new ItemOnClick());

        builder = new AlertDialog.Builder(this);
        builder.setTitle("请选择查询类型");
        builder.setIcon(R.drawable.ic_launcher);
        //设置加载的listview
        builder.setView(getlistview);
        builder.setPositiveButton("确定", new DialogOnClick());
        builder.setNegativeButton("取消", new DialogOnClick());
        show = builder.show();


    }

    List<String> ccList = new ArrayList<String>();

    class DialogOnClick implements DialogInterface.OnClickListener {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case Dialog.BUTTON_POSITIVE:
                    ccList.clear();
                    for (int i = 0; i < bl.size(); i++) {
                        Log.e("111", "选择" + i + ":" + bl.get(i));
                        if (bl.get(i) == true) {
                            ccList.add(queryall.get(i).getName());
                        } else {

                        }

                    }
                    for (int i = 0; i < ccList.size(); i++) {
                        List<Optionals> querycunzai = z_optionals_dao.querycunzai(dataBean.getName(), ccList.get(i));
                        if (querycunzai.size()== 0) {
                            z_optionals_dao.addMSG(ccList.get(i), dataBean.getName(), dataBean.getPinyin(), dataBean.getGainian(), dataBean.getIndustry(), dataBean.getArea());
                        }else {

                        }
                        Toast.makeText(ListViewActivity.this, "" + ccList.get(i), Toast.LENGTH_SHORT).show();




                    }


                    break;
                case Dialog.BUTTON_NEGATIVE:
                    //取消按钮的事件
                    break;
                default:
                    break;
            }
        }
    }

    public void addgroup() {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("请输入");    //设置对话框标题
        builder.setIcon(android.R.drawable.btn_star);   //设置对话框标题前的图标
        final EditText edit = new EditText(context);
        builder.setView(edit);
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                List<Groups> ischunzai = z_groups_dao.ischunzai(edit.getText().toString());
                if (ischunzai != null && ischunzai.size() > 0) {
                    Toast.makeText(context, "已经存在", Toast.LENGTH_SHORT).show();
                } else {
                    z_groups_dao.addGroups(edit.getText().toString());
                }

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(context, "你点了取消", Toast.LENGTH_SHORT).show();
            }
        });

        builder.show();
    }

    //重写simpleadapterd的getview方法
    class SetSimpleAdapter extends SimpleAdapter {

        public SetSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from,
                                int[] to) {
            super(context, data, resource, from, to);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LinearLayout.inflate(getBaseContext(), R.layout.listitem, null);
            }
            CheckBox ckBox = (CheckBox) convertView.findViewById(R.id.X_checkbox);
            //每次都根据 bl[]来更新checkbox
            if (bl.get(position) == true) {
                ckBox.setChecked(true);
            } else if (bl.get(position) == false) {
                ckBox.setChecked(false);
            }
            return super.getView(position, convertView, parent);
        }
    }
}
